//
//  BPCouponViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/2/6.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPCouponViewController.h"
#import "BPCouponTableViewCell.h"

@interface BPCouponViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableview;
@property (strong, nonatomic) NSArray *data;

@end

@implementation BPCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"优惠券";
    self.view.backgroundColor = [UIColor colorWithRed:0xec / 255.0 green:0xf5 / 255.0 blue:0xfc / 255.0 alpha:1.0];
    [self getData];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}

- (void)getData {
    [SVProgressHUD showWithStatus:@"读取中..."];
    NSDictionary *user = [[NSUserDefaults standardUserDefaults]objectForKey:@"user"];
    AFHTTPSessionManager *mgrName = [AFHTTPSessionManager manager];
    mgrName.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         nil];
    
    [mgrName POST:[NSString stringWithFormat:@"%@?c=Coupon&&a=pList",requestURL]
       parameters:@{@"token" : user[@"token"],
                    @"page" : @"1",
                    @"pagesize" : @"20"}
         progress:^(NSProgress * _Nonnull uploadProgress) {
         } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             NSLog(@"优惠券 %@", responseObject);
             if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
                 [SVProgressHUD dismiss];
                 self.data = responseObject[@"data"];
                 
                 UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, screenW, screenH - 64)];
                 self.tableview = tableview;
                 tableview.backgroundColor = [UIColor clearColor];
                 tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
                 [tableview registerNib:[UINib nibWithNibName:@"BPCouponTableViewCell" bundle:nil] forCellReuseIdentifier:@"couponCell"];
                 tableview.dataSource = self;
                 tableview.delegate = self;
                 [self.view addSubview:tableview];
             }
             else {
                 [SVProgressHUD showInfoWithStatus:@"请求失败"];
                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                     [SVProgressHUD dismiss];
                 });
             }
         } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             [SVProgressHUD showInfoWithStatus:@"网络故障"];
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 [SVProgressHUD dismiss];
             });
         }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BPCouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"couponCell"];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.data = self.data[indexPath.row];
    
    cell.couponType = CouponTypeMoney;
    
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //根据该券可用与否判定是否可点击
    long canUse = [self.data[indexPath.row][@"canUse"] longValue];
    if (canUse) {
        if ([self.delegate respondsToSelector:@selector(selectedCoupon:)]) {
            [self.delegate selectedCoupon:self.data[indexPath.row]];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}



@end
